using Microsoft.Extensions.FileProviders;
using Project_Schedule.Configures;
using NLog.Web;
using Project_Schedule.Domain.BackGround.com.core.Interface;
using Project_Schedule.Domain.BackGround.com.core.Service;
using Project_Schedule.Domain.Receptionist.com.core.Interface;
using Project_Schedule.Domain.Receptionist.com.core.Service;
using Project_Schedule.Model;

namespace Project_Schedule;

public static class Assemble_System
{
    public static IConfiguration? Configuration { get; private set; }

    public static void Assemble_Scoped(WebApplicationBuilder builder)
    {
        builder.Services.AddScoped<ISystemRegister, System_RegisterService>();
        builder.Services.AddScoped<ISystemLogin, System_LoginService>();
        builder.Services.AddScoped<ISystemGetRefuseUserMessage, System_GetRefuseUserMesService>();
        builder.Services.AddScoped<ISystemChangeRefuseUser, System_ChangeWebRegisterUser>();

        builder.Services.AddScoped<IBackgroundRegister, Bg_RegisterService>();
        builder.Services.AddScoped<IBackgroundCreateUser, Bg_CreateUserService>();
        builder.Services.AddScoped<IBackgroundAuditSchUser, Bg_AuditReceptionistService>();
        builder.Services.AddScoped<IBackgroundStartUsing, Bg_StartUsingService>();
        builder.Services.AddScoped<IBackgroundRefuseUserSendMessage, Bg_RefuseWebErrorMessageService>();
    }

    public static void Assemble_Transient(WebApplicationBuilder builder)
    {
        builder.Services.AddTransient<DataDbContext>();
        builder.Services.AddTransient<IBackgroundUserQuerySass, Bg_UserQuerySassService>();
    }

    public static void Assemble_Singleton(WebApplicationBuilder builder)
    {
        Assemble_System.Configuration = builder.Configuration;
        builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        builder.Services.AddSingleton<INLogHelper, NLogHelper>();
    }

    public static void Assemble_Service(WebApplicationBuilder builder)
    {
        builder.Logging.AddNLog("Configures/NLog.config");
        builder.Services.AddControllers(options =>
            options.Filters.Add(typeof(CustomerGlobalExceptionFilterAsync)));
        builder.Services.AddEndpointsApiExplorer();
        builder.Services.AddSwaggerGen();
        builder.Services.AddCors(options =>
        {
            options.AddPolicy(name: "myCors",
                build =>
                {
                    build.WithOrigins("*", "*", "*")
                        .AllowAnyOrigin()
                        .AllowAnyHeader()
                        .AllowAnyMethod();
                });
        });
    }

    public static void Assemble_StartUp(WebApplication application)
    {
        if (application.Environment.IsDevelopment())
        {
            application.UseSwagger();
            application.UseSwaggerUI();
        }

        application.UseCors("myCors");

        application.UseHttpsRedirection();

        //var file =
        //   new PhysicalFileProvider(
        //     Path.Combine(Directory.GetCurrentDirectory(),
        //        "StaticFiles"));


        application.UseStaticFiles(new StaticFileOptions()
        {
            FileProvider = new PhysicalFileProvider(
                Path.Combine(Directory.GetCurrentDirectory(),
                    "StaticFiles"))
        });

        application.UseAuthorization();

        application.MapControllers();
    }
}